﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using NC.Cather;
using NC.Infrastructure.Domain;

namespace NC.Infrastructure.Data
{
    public static class DbExtension
    {
        public static IQueryable<TEntity> Include<TEntity>(this IQueryable<TEntity> dbset, params string[] includes)
        {
            foreach (var path in includes)
            {
                dbset = dbset.Include(path);
            }
            return dbset;
        }

        public static IQueryable<TEntity> PageList<TEntity>(this IQueryable<TEntity> entities, int page,int size=10) where TEntity : BaseEntity
        {
            return PageList<TEntity, DateTime>(entities, page, n => n.CreatedAt, size);
        }

        public static IQueryable<TEntity> PageList<TEntity, OrderKey>(this IQueryable<TEntity> entities, int page, Expression<Func<TEntity, OrderKey>> order,int size=10) where TEntity : BaseEntity
        {
            return entities.OrderByDescending(order).Skip(page*size).Take(size);
        }
    }
}
